package com.docmgmt.mvp.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.nio.charset.StandardCharsets;

/**
 * Jackson 配置
 */
@Configuration
public class JacksonConfig {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();

        // 使用标准驼峰命名（与Knife4j、前端一致）
        // 移除了SNAKE_CASE配置，改为默认驼峰命名

        // 忽略未知属性（提高兼容性）
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        
        // 允许字符串中包含单引号
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

        // 注册Java 8时间模块（支持LocalDateTime等类型的序列化）
        mapper.registerModule(new JavaTimeModule());

        // 禁用将日期写为时间戳（使用ISO-8601格式）
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        // 设置默认字符集为UTF-8
        mapper.getFactory().setCharacterEscapes(null);
        
        return mapper;
    }
}